"""
工单管理Schema模型
用于接口请求和响应的数据结构定义
"""
from datetime import datetime
from typing import Optional, List, Any

from pydantic import BaseModel, Field, ConfigDict

from app.models.work_order_models import WorkOrderLog
from common.base_enums import (
	WorkOrderStatus, WorkOrderPriority, WorkOrderSourceType,
	WorkOrderDeviceType, WorkOrderAction
)


# ============ 工单基础模型 ============

class WorkOrderBase(BaseModel):
	"""工单基础模型"""
	title: str = Field(..., description="标题", max_length=128)
	source_type: WorkOrderSourceType = Field(..., description="来源类型")
	alarm_id: Optional[int] = Field(None, description="关联告警ID")
	device_type: Optional[WorkOrderDeviceType] = Field(None, description="目标设备类型")
	device_id: Optional[int] = Field(None, description="目标设备ID")
	priority: WorkOrderPriority = Field(WorkOrderPriority.MEDIUM, description="优先级")
	description: Optional[str] = Field(None, description="问题描述")


class WorkOrderCreate(WorkOrderBase):
	"""创建工单请求模型"""
	pass


class WorkOrderUpdate(BaseModel):
	"""更新工单请求模型"""
	title: Optional[str] = Field(None, description="标题", max_length=128)
	priority: Optional[WorkOrderPriority] = Field(None, description="优先级")
	description: Optional[str] = Field(None, description="问题描述")


class WorkOrderResponse(WorkOrderBase):
	"""工单响应模型"""
	id: int
	code: str = Field(..., description="工单编码")
	status: WorkOrderStatus = Field(..., description="状态")
	assignee_code: Optional[str] = Field(None, description="负责人编码")
	expect_finish_time: Optional[datetime] = Field(None, description="期望完成时间")
	solution: Optional[str] = Field(None, description="解决方案")
	close_comment: Optional[str] = Field(None, description="关闭说明")
	closed_by: Optional[str] = Field(None, description="关闭人")
	closed_at: Optional[datetime] = Field(None, description="关闭时间")
	ext: Optional[Any] = Field(None, description="扩展字段")
	created_at: datetime
	updated_at: datetime

	model_config = ConfigDict(from_attributes=True)


# ============ 工单分派模型 ============

class WorkOrderDispatchRequest(BaseModel):
	"""工单分派请求模型"""
	assignee_code: str = Field(..., description="负责人编码")
	expect_finish_time: Optional[datetime] = Field(None, description="期望完成时间")
	comment: Optional[str] = Field(None, description="备注")


# ============ 工单处理模型 ============

class WorkOrderProcessRequest(BaseModel):
	"""工单处理请求模型"""
	comment: Optional[str] = Field(None, description="处理说明")


# ============ 工单解决模型 ============

class WorkOrderResolveRequest(BaseModel):
	"""工单解决请求模型"""
	solution: str = Field(..., description="解决方案")
	comment: Optional[str] = Field(None, description="备注")


# ============ 工单关闭模型 ============

class WorkOrderCloseRequest(BaseModel):
	"""工单关闭请求模型"""
	close_comment: Optional[str] = Field(None, description="关闭说明")


# ============ 查询相关模型 ============

class WorkOrderQuery(BaseModel):
	"""工单查询参数"""
	page: int = Field(1, ge=1, description="页码")
	size: int = Field(20, ge=1, le=100, description="每页大小")
	status: Optional[List[WorkOrderStatus]] = Field(None, description="状态列表筛选")
	assignee_code: Optional[str] = Field(None, description="负责人筛选")
	device_id: Optional[int] = Field(None, description="设备ID筛选")
	source_type: Optional[WorkOrderSourceType] = Field(None, description="来源类型筛选")
	station_id: Optional[int] = Field(None, description="台区ID筛选"),
	start_time: Optional[datetime] = None,
	end_time: Optional[datetime] = None,


class WorkOrderListResponse(BaseModel):
	"""工单列表响应模型"""
	total: int
	items: List[WorkOrderResponse]


# ============ 工单详情模型 ============

class WorkOrderDetailResponse(WorkOrderResponse):
	"""工单详情响应模型（包含日志）"""
	logs: List[WorkOrderLog] = Field(default_factory=list, description="工单流转日志")


# ============ 工单日志模型 ============

class WorkOrderLogResponse(BaseModel):
	"""工单日志响应模型"""
	id: int
	wo_id: int
	action: WorkOrderAction
	actor_code: str
	actor_name: Optional[str]
	comment: Optional[str]
	old_status: Optional[WorkOrderStatus]
	new_status: Optional[WorkOrderStatus]
	created_at: datetime

	model_config = ConfigDict(from_attributes=True)


class WorkOrderTimelineResponse(BaseModel):
	"""工单时间线响应模型"""
	items: List[WorkOrderLogResponse]
